using Android.App;
using Android.OS;
using Android.Widget;
using AF.Data;
using AF.Models;

namespace AF.Activities
{
    [Activity(Label = "新建项目", Theme = "@android:style/Theme.Material.Light.NoActionBar")]
    public class AddProjectActivity : Activity
    {
        private DatabaseHelper? dbHelper;
        private EditText? editProjectName;
        private EditText? editClientName;
        private EditText? editBudget;
        private Button? btnSelectStartDate;
        private EditText? editDescription;
        private EditText? editRemark;
        private Button? btnSave;
        private Button? btnCancel;

        private DateTime selectedStartDate = DateTime.Now;

        protected override void OnCreate(Bundle? savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_add_project);

            dbHelper = new DatabaseHelper(this);

            InitializeViews();
            SetupEventHandlers();
        }

        private void InitializeViews()
        {
            editProjectName = FindViewById<EditText>(Resource.Id.editProjectName);
            editClientName = FindViewById<EditText>(Resource.Id.editClientName);
            editBudget = FindViewById<EditText>(Resource.Id.editBudget);
            btnSelectStartDate = FindViewById<Button>(Resource.Id.btnSelectStartDate);
            editDescription = FindViewById<EditText>(Resource.Id.editDescription);
            editRemark = FindViewById<EditText>(Resource.Id.editRemark);
            btnSave = FindViewById<Button>(Resource.Id.btnSave);
            btnCancel = FindViewById<Button>(Resource.Id.btnCancel);

            // 设置默认日期
            btnSelectStartDate!.Text = selectedStartDate.ToString("yyyy年MM月dd日");
        }

        private void SetupEventHandlers()
        {
            if (btnSelectStartDate != null)
            {
                btnSelectStartDate.Click += (s, e) => ShowDatePicker();
            }

            if (btnSave != null)
            {
                btnSave.Click += (s, e) => SaveProject();
            }

            if (btnCancel != null)
            {
                btnCancel.Click += (s, e) => Finish();
            }
        }

        private void ShowDatePicker()
        {
            var datePickerDialog = new DatePickerDialog(
                this,
                (s, e) =>
                {
                    selectedStartDate = e.Date;
                    if (btnSelectStartDate != null)
                        btnSelectStartDate.Text = selectedStartDate.ToString("yyyy年MM月dd日");
                },
                selectedStartDate.Year,
                selectedStartDate.Month - 1,
                selectedStartDate.Day
            );
            datePickerDialog.Show();
        }

        private void SaveProject()
        {
            if (dbHelper == null) return;

            // 验证输入
            if (string.IsNullOrWhiteSpace(editProjectName?.Text))
            {
                Toast.MakeText(this, "请输入项目名称", ToastLength.Short)?.Show();
                return;
            }

            decimal? budget = null;
            if (!string.IsNullOrWhiteSpace(editBudget?.Text))
            {
                if (decimal.TryParse(editBudget?.Text, out decimal budgetValue) && budgetValue > 0)
                {
                    budget = budgetValue;
                }
                else
                {
                    Toast.MakeText(this, "请输入有效的预算金额", ToastLength.Short)?.Show();
                    return;
                }
            }

            var project = new Project
            {
                Name = editProjectName?.Text ?? "",
                Description = editDescription?.Text ?? "",
                ClientName = editClientName?.Text,
                Budget = budget,
                Status = ProjectStatus.InProgress,
                StartDate = selectedStartDate,
                Remark = editRemark?.Text
            };

            long result = dbHelper.AddProject(project);

            if (result > 0)
            {
                Toast.MakeText(this, "项目创建成功", ToastLength.Short)?.Show();
                SetResult(Result.Ok);
                Finish();
            }
            else
            {
                Toast.MakeText(this, "项目创建失败", ToastLength.Short)?.Show();
            }
        }
    }
}

